#include "Test.h"
#include "TestResult.h"
#include "TestListener.h"
#include "TestFailure.h"

CPPUNITLITE3_NS_BEGIN;

TextOutputter::TextOutputter(const TestResult* result): 
    m_iCnt(0),
    m_pResult(result),
    m_bShowSuccess(false)
{
    memset(message, 0, 1024);
}
    
TextOutputter::~TextOutputter() 
{
}

void TextOutputter::testsStarted (int totalNumber, int repeat)
{
    fprintf(stderr, "There are %d tests, and will be run for %d time(s)\n", totalNumber, repeat);
    fprintf (stderr, "No|Result|TestName|ElapsedTime|FileName|LineNumber|Message|\n");
}

void TextOutputter::testsEnded ()
{
    fprintf(stderr, 
        "Failure:   %d\n"
        "All run:   %d\n"
        "Elapsed Time: %lld(us)\n",
        m_pResult->getFailureCount(),
        m_pResult->getAllRun(),
        m_pResult->getTotalElapsedTime());
}

void TextOutputter::StartTest(Test* test) 
{
}

void TextOutputter::EndTest(Test* test) 
{
    if(m_pResult->wasSuccessful())
    {
        if(m_bShowSuccess)
        {
            fprintf(stderr, "%d|%s|%s|%lld||||\n",
                ++m_iCnt, 
                "Success", 
                test->getName(), 
                m_pResult->getElapsedTime());
        }
    }
    else
    {
        fprintf(stderr, "\033[0;3%sm", "1");// 1 red; 2 green; 3 yellow
        fprintf(stderr, "%d|%s|%s|%lld|%s\n",
            ++m_iCnt, 
            "Success", 
            test->getName(), 
            m_pResult->getElapsedTime(), 
            message);
        fprintf(stderr, "\033[m");  // Resets the terminal to default.
    }
}

void TextOutputter::addFailure(const TestFailure& failure)
{
    sprintf(message, "%s|%d|%s|", failure.getFileName(), 
        failure.getLineNumber(), failure.getMessage());
}

void TextOutputter::setShowMode(bool bShowSuccess)
{
    m_bShowSuccess = bShowSuccess;
}

CPPUNITLITE3_NS_END;

